package ${pro.basePath}.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vonchange.headb.core.map.HeaMap;
import com.vonchange.headb.core.page.IPage;
import ${pro.entityPath}.${bean.entityName};
import ${pro.basePath}.service.I${bean.entityName}Service;
import com.vonchange.utils.page.PageUtils;
import com.vonchange.utils.web.RequestUtils;
import com.vonchange.utils.web.Result;
@Controller
@RequestMapping("/${bean.entityName?uncap_first}")
public class ${bean.entityName!}Action {
	@Resource
	private I${bean.entityName!}Service ${bean.entityName?uncap_first}Service;

	@RequestMapping("/findById")
	@ResponseBody
	public ${bean.entityName} findById(@RequestParam("id")${bean.idType} id){
		return ${bean.entityName?uncap_first}Service.findById(id);
	}
	@RequestMapping("/findList")
	@ResponseBody
	public List<${bean.entityName}> findList(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> param =RequestUtils.getQueryParams(request);
		return ${bean.entityName?uncap_first}Service.findList(new HeaMap().setAll(param));
	}
	@RequestMapping("/findPage")
	@ResponseBody
	public IPage<${bean.entityName}> findPage(Integer pageNo,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
		IPage<${bean.entityName}> page=PageUtils.initPage(pageNo, pageSize);
		Map<String, Object> param =RequestUtils.getQueryParams(request);
		return ${bean.entityName?uncap_first}Service.findPage(page,new HeaMap().setAll(param));
	}
	@RequestMapping("/delete")
	@ResponseBody
	public Result deleteById(@RequestParam("id")${bean.idType} id){
		int result=${bean.entityName?uncap_first}Service.deleteById(id);
		if(result==0){
			return new Result().setStatusFail().setMsg("删除失败");
		}
		return new Result().setMsg("删除成功");
	}
	@RequestMapping("/add")
	@ResponseBody
	public Result add(${bean.entityName} ${bean.entityName?uncap_first},HttpServletRequest request,HttpServletResponse response){
		if(null==${bean.entityName?uncap_first}){
			return new Result().setStatusFail().setMsg("请传入数据");
		}
		String msg="增加";
		${bean.entityName?uncap_first} =${bean.entityName?uncap_first}Service.save(${bean.entityName?uncap_first});
		return new Result().setResult(${bean.entityName?uncap_first}).setMsg(msg+"成功");
	}   
	@RequestMapping("/update")
	@ResponseBody
	public Result update(${bean.entityName} ${bean.entityName?uncap_first},HttpServletRequest request,HttpServletResponse response){
		if(null==${bean.entityName?uncap_first}){
			return new Result().setStatusFail().setMsg("请传入数据");
		}
		String msg="修改";
		if(null==${bean.entityName?uncap_first}.get${bean.idName?cap_first}()){
			return new Result().setStatusFail().setMsg("请输入ID");
		}
		int result=${bean.entityName?uncap_first}Service.update(${bean.entityName?uncap_first});
		if(result<=0){
			return new Result().setStatusFail().setMsg(msg+"未成功");
		}
		return new Result().setResult(${bean.entityName?uncap_first}).setMsg(msg+"成功");
	}     


}
